<template>
  <view class="container">
    <!-- 地址列表 -->
    <view class="address-list">
      <view 
        class="address-item" 
        v-for="(item, index) in addressList" 
        :key="index"
      >
        <view class="info" @tap="selectAddress(item)">
          <view class="user-info">
            <text class="name">{{item.name}}</text>
            <text class="phone">{{item.phone}}</text>
            <text class="tag" v-if="item.isDefault">默认</text>
          </view>
          <view class="address">{{item.province}}{{item.city}}{{item.district}}{{item.detail}}</view>
        </view>
        <view class="actions">
          <view class="action-item" @tap="editAddress(item)">
            <text class="iconfont icon-edit"></text>
            <text>编辑</text>
          </view>
          <view class="action-item" @tap="deleteAddress(item)">
            <text class="iconfont icon-delete"></text>
            <text>删除</text>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 空状态 -->
    <view class="empty" v-if="addressList.length === 0">
      <image src="/static/images/empty-address.png" mode="aspectFit"></image>
      <text>暂无收货地址</text>
    </view>
    
    <!-- 新增按钮 -->
    <view class="add-section">
      <button class="add-btn" @tap="addAddress">
        <text class="iconfont icon-add"></text>
        <text>新增收货地址</text>
      </button>
    </view>
  </view>
</template>

<script>
import { addressApi } from '@/api/index'
import { showToast, showModal } from '@/utils/common'

export default {
  data() {
    return {
      addressList: [],
      fromOrder: false
    }
  },
  onLoad(options) {
    this.fromOrder = options.from === 'order'
    this.getAddressList()
  },
  methods: {
    // 获取地址列表
    async getAddressList() {
      try {
        const res = await addressApi.getAddressList()
        this.addressList = res
      } catch (error) {
        showToast('地址列表加载失败')
      }
    },
    
    // 选择地址
    selectAddress(address) {
      if (!this.fromOrder) return
      
      const pages = getCurrentPages()
      const prevPage = pages[pages.length - 2]
      prevPage.$vm.setAddress(address)
      uni.navigateBack()
    },
    
    // 新增地址
    addAddress() {
      uni.navigateTo({
        url: '/pages/address/edit'
      })
    },
    
    // 编辑地址
    editAddress(address) {
      uni.navigateTo({
        url: `/pages/address/edit?id=${address.id}`
      })
    },
    
    // 删除地址
    async deleteAddress(address) {
      try {
        await showModal('提示', '确定要删除该地址吗？')
        await addressApi.deleteAddress(address.id)
        showToast('删除成功')
        this.getAddressList()
      } catch (error) {
        if (error) {
          showToast('删除失败')
        }
      }
    }
  }
}
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 120rpx;
}

.address-list {
  padding: 20rpx;
  
  .address-item {
    background: #fff;
    border-radius: 12rpx;
    padding: 30rpx;
    margin-bottom: 20rpx;
    
    .info {
      .user-info {
        margin-bottom: 10rpx;
        
        .name {
          font-size: 32rpx;
          color: #333;
          margin-right: 20rpx;
        }
        
        .phone {
          font-size: 28rpx;
          color: #666;
        }
        
        .tag {
          font-size: 24rpx;
          color: #ff0000;
          border: 1rpx solid #ff0000;
          padding: 4rpx 10rpx;
          border-radius: 4rpx;
          margin-left: 20rpx;
        }
      }
      
      .address {
        font-size: 28rpx;
        color: #666;
      }
    }
    
    .actions {
      display: flex;
      justify-content: flex-end;
      margin-top: 20rpx;
      padding-top: 20rpx;
      border-top: 1rpx solid #f5f5f5;
      
      .action-item {
        display: flex;
        align-items: center;
        margin-left: 30rpx;
        
        .iconfont {
          font-size: 32rpx;
          color: #666;
          margin-right: 6rpx;
        }
        
        text {
          font-size: 28rpx;
          color: #666;
        }
      }
    }
  }
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 200rpx;
  
  image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
  }
  
  text {
    font-size: 28rpx;
    color: #999;
  }
}

.add-section {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 20rpx 30rpx;
  background: #fff;
  
  .add-btn {
    width: 100%;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    background: #ff0000;
    color: #fff;
    border-radius: 40rpx;
    font-size: 28rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .iconfont {
      font-size: 32rpx;
      margin-right: 10rpx;
    }
  }
}
</style> 